package com.nimitz.web.controller.task;

import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.domain.entity.SysUser;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.common.utils.SecurityUtils;
import com.nimitz.task.domain.Task;
import com.nimitz.task.domain.TaskResult;
import com.nimitz.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/smart/task")
public class TaskController extends BaseController{
    @Autowired
    private TaskService taskService;

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('smart:task:list')")
    public TableDataInfo selectAll(@RequestParam(required = false) Long auditorId, @RequestParam(required = false) String state){
        startPage();
        List<Task> list = taskService.selectAll(auditorId,state);
        return getDataTable(list);
    }

    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('smart:task:edit')")
    public AjaxResult updateTask(@RequestBody Task task){
        Long userId = SecurityUtils.getUserId();
        task.setUpdateBy(userId);
        taskService.updateTask(task);
        return AjaxResult.success();
    }

    @PutMapping("/updateAuditor")
    @PreAuthorize("@ss.hasPermi('smart:task:edit1')")
    public AjaxResult updateAuditor(@RequestBody Task task){
        Long userId = SecurityUtils.getUserId();
        task.setUpdateBy(userId);
        taskService.updateAuditor(task);
        return AjaxResult.success();
    }
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('smart:task:add')")
    public AjaxResult addTask(@RequestBody Task task){
        Long userId = SecurityUtils.getUserId();
        task.setCreateBy(userId);
        task.setUpdateBy(userId);
        taskService.addTask(task);
        return AjaxResult.success();
    }

    @GetMapping("/select")
    @PreAuthorize("@ss.hasPermi('smart:task:select')")
    public AjaxResult selectById(@RequestParam Long id){
        TaskResult taskResult = taskService.selectById(id);
        return AjaxResult.success(taskResult);
    }

    @PutMapping("/delete")
    @PreAuthorize("@ss.hasPermi('smart:task:remove')")
    public AjaxResult deleteById(@RequestParam Long id){
        taskService.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/countAll")
    public AjaxResult countAll(){
        int countAll = taskService.countAll();
        return AjaxResult.success(countAll);
    }

    @GetMapping("/countCompleted")
    public AjaxResult countCompleted(){
        int countCompleted = taskService.countCompleted();
        return AjaxResult.success(countCompleted);
    }

    @GetMapping("/lookFor")
    public AjaxResult selectByRoleId(){
        List<SysUser> list = taskService.selectByRoleId();
        return AjaxResult.success(list);
    }
}
